<template>
  <div class="container">
    <div class="login-container">
      <img src="../assets/logo.png" alt class="login-logo" />
      <div class="login-form">
        <!-- // 验证规则步骤②:绑定验证规则到表单,:rules -->
        <el-form ref="loginFormRefs" :model="loginForm" label-width="80px" :rules="loginFormRules">
          <!-- // 验证规则步骤③:选择每一个输入框的绑定规则 -->
          <el-form-item label="用户名:" prop="username">
            <el-input v-model="loginForm.username" prefix-icon="fa fa-user"></el-input>
          </el-form-item>
          <el-form-item label="密码:" prop="password">
            <el-input v-model="loginForm.password" type="password" prefix-icon="fa fa-key"></el-input>
          </el-form-item>
          <el-row class="btn-group">
            <el-button type="primary" @click="login">登陆</el-button>
            <el-button type="default" @click="resetLoginForm">取消</el-button>
          </el-row>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script>
  export default {
    data() {
      return {
        loginForm: {
          username: 'admin',
          password: '123456'
        },
        // 验证规则步骤①:创建验证规则
        loginFormRules: {
          // 用户名验证规则
          username: [{
              required: true,
              message: '请输入用户名',
              trigger: 'blur'
            },
            {
              min: 3,
              max: 20,
              message: '长度在 3 到 20 个字符',
              trigger: 'blur'
            }
          ],
          // 密码验证规则
          password: [{
              required: true,
              message: '请输入密码',
              trigger: 'blur'
            },
            {
              min: 6,
              max: 20,
              message: '密码长度 6 到 20 个字符',
              trigger: 'blur'
            }
          ]
        }
      }
    },
    methods: {
      // 表单登陆前验证
      login() {
        this.$refs.loginFormRefs.validate(async valid => {
          if (!valid) {
            return
          }
          // 如果promise，则添加await
          // { data: result } 这种写法直接将result中我们所需要的data提取出来
          const result = await this.$http.post('login', this.loginForm)
          if (result.meta.status !== 200) return this.$message.error(result.meta.msg)
          this.$message.success(result.meta.msg)
          // 保存token，记录登录状态
          window.sessionStorage.setItem('token', result.data.token)
          // 页面跳转到home
          this.$router.push('/home')
        })
      },
      // 重置表单
      resetLoginForm() {
        this.$refs.loginFormRefs.resetFields()
      }
    }
  }
</script>

<style lang="less" scoped>
  .container {
    width: 100%;
    height: 100%;
    background-color: #2b4b6b;
  }

  .login-container {
    width: 500px;
    height: 300px;
    background-color: #fff;
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);

    .login-logo {
      width: 120px;
      height: 120px;
      background-color: #eee;
      border-radius: 50%;
      position: absolute;
      box-shadow: 0 0 10px #ddd;
      left: 50%;
      transform: translate(-50%, -50%);
    }

    .login-form {
      margin-top: 60px;
      padding: 20px;
    }

    .btn-group {
      display: flex;
      justify-content: flex-end;
    }
  }
</style>
